/*
 * @Author: shijianxin
 * @Date: 2022-04-24 11:07:20
 * @Description: 电影列表页
 */

import React, { Dispatch } from "react";
import MovieTable, { IMovieTableEvents } from "../../components/MovieTable";
import { connect } from "react-redux";
import { IRootState } from "../../redux/reducers/RootReducer";
import MovieAction, { MovieActions } from "../../redux/actions/MovieAction";
import { IMovieState } from "../../redux/reducers/MovieReducer";

function mapStateToProps(state: IRootState): IMovieState {
  return state.movie;
}

function mapDispatchToProps(dispatch: Dispatch<any>): IMovieTableEvents {
  return {
    onLoad() {
      dispatch(
        MovieAction.fetchMovies({
          page: 1,
          limit: 10,
          key: "",
        })
      );
    },
    onSwitchChange(type, newState, id) {
      dispatch(MovieAction.changeSwitch(type, newState, id));
    },
    async onDelete(id) {
      await dispatch(MovieAction.deleteMovie(id));
    },
    onChange(newPage) {
      dispatch(
        MovieAction.fetchMovies({
          page: newPage,
        })
      );
    },
    onKeyChange(key) {
      dispatch(
        MovieAction.SetConditionAction({
          key,
        })
      );
    },
    onSearch() {
      dispatch(
        MovieAction.fetchMovies({
          page: 1,
        })
      );
    },
  };
}

export default connect(mapStateToProps, mapDispatchToProps)(MovieTable);
